<?php

namespace Newsletter\NewsBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class NewsletterController extends Controller
{
    public function indexAction()
    {
		 
		 $em = $this->getDoctrine()->getManager();


     //   $newsletters = $em->getRepository('NewsletterNewsBundle:Newsletter')->getLatestNewsletters();

	    $newsletters = $em->getRepository('NewsletterNewsBundle:Newsletter')->findBy(
			 array(), 
             array('id' => 'DESC')	
		);


        return $this->render('NewsletterNewsBundle:Newsletter:index.html.twig', array(
            'newsletters' => $newsletters
		));
	

    }



	/**
     * Show a newsletter entry
     */
    public function showAction($id, $slug, $plain=false)
    {


        $em = $this->getDoctrine()->getManager();

        $newsletter = $em->getRepository('NewsletterNewsBundle:Newsletter')->find($id);

        if (!$newsletter) {
            throw $this->createNotFoundException('Unable to find newsletter.');
        }


		if ($plain)
		{
			echo $newsletter->getBodyHtml();
			exit();
		//	$this->setLayout(false);
			// return $this->renderText($this->newsletter->getBodyHtml());
			//return $this->render("test");
		}

		$comments = array();

		// $comments = $em->getRepository('NewsletterNewsBundle:Comment')->getCommentsForNewsletter($newsletter->getId());

		return $this->render('NewsletterNewsBundle:Newsletter:show.html.twig', array(
			'newsletter'      => $newsletter,
			'comments'  => $comments
		));
    }




	public function sidebarAction()
	{

		$latestComments = array();
	
		$em = $this->getDoctrine()
				   ->getManager();

		$tags = $em->getRepository('NewsletterNewsBundle:Newsletter')
				   ->getTags();

		$tagWeights = $em->getRepository('NewsletterNewsBundle:Newsletter')
						 ->getTagWeights($tags);

		/*		 
		$commentLimit   = $this->container
                           ->getParameter('blogger_blog.comments.latest_comment_limit');
		$latestComments = $em->getRepository('NewsletterNewsBundle:Comment')
							 ->getLatestComments($commentLimit);
	return $this->render('NewsletterNewsBundle:Page:sidebar.html.twig', array(
			'latestComments'    => $latestComments,
			'tags'              => $tagWeights
		));
	
		*/

			return $this->render('NewsletterNewsBundle:Newsletter:sidebar.html.twig', array(
			'latestComments'    => $latestComments,
			'tags'              => $tagWeights
		));
	}



}
